<?php
function greengar_search_drawings(
        $from_id = null,
	$to_id = null,
	$title = null,
	$user_ids = null,
	$before_time = null,
	$after_time = null,
	$device_model = null,
	$device_uuid = null,
	$location = null,
	$tags = null,
        
        $drawings_count = 10,
        $offset = 0,
        $orderby = null,
        $orderad = 'desc',

        $mentions = null,
        $highlights = null,
        $shared = 0 // PUBLIC
)
{
	$pairs = array();	
	if($from_id != null)
		$pairs[] = array('name' => 'guid', 'value' => $from_id, 'operand' => '>=', 'case_sensitive' => false);
		
	if($to_id != null)
		$pairs[] = array('name' => 'guid', 'value' => $to_id, 'operand' => '<=', 'case_sensitive' => false);
	
	if($title != null)
		$pairs[] = array('name' => 'title', 'value' => '%'.$title.'%', 'operand' => 'LIKE', 'case_sensitive' => false);
		
	/*if($user_ids !== null)
	{
		foreach ($user_ids as $id)
		{
			$pairs[] = array('name' => 'collaborator_ids', 'value' => '%'.$id.'%', 'operand' => 'LIKE', 'case_sensitive' => false);
		}
	}*/
	if($device_model != null)
		$pairs[] = array('name' => 'device_model', 'value' => '%'.$device_model.'%', 'operand' => 'LIKE', 'case_sensitive' => false);
		
	if($device_uuid != null)
		$pairs[] = array('name' => 'device_uuid', 'value' => $device_uuid, 'operand' => '=', 'case_sensitive' => false);
		
	if($location != null)
		$pairs[] = array('name' => 'location', 'value' => $location, 'operand' => 'LIKE', 'case_sensitive' => false);
		
	if($tags != null)
		$pairs[] = array('name' => 'tags', 'value' => $tags, 'operand' => '=', 'case_sensitive' => false);
//        
//        if($mentions != null)
//		$pairs[] = array('name' => 'mentions', 'value' => $mentions, 'operand' => '=', 'case_sensitive' => false);
//        
//        if($highlights != null)
//		$pairs[] = array('name' => 'highlights', 'value' => $highlights, 'operand' => '=', 'case_sensitive' => false);
        
        $pairs[] = array('name' => 'shared', 'value' => $shared, 'operand' => '<=', 'case_sensitive' => false);
        
        $orderad = strtoupper($orderad);

        $pairs[] = array('name' => WHITEBOARD_DRAWING_METADATA, 'value' => 1, 'operand' => '=', 'case_sensitive' => false);
	
        switch ($orderby)
        {
            case 'guid':
            case 'likenum':
                $orderas = 'integer';
                break;
            default:
                $orderas = 'text';
                break;
        }
        
        $options = array(
		'types' => 'object',
		'subtypes' => WHITEBOARD_DRAWING_SUBTYPE,
            
                'limit' => $drawings_count,
                'offset' => $offset,
            
		'created_time_upper' => $before_time,
		'created_time_lower' => $after_time,
            
		'metadata_name_value_pairs' => $pairs,
                
		'metadata_case_sensitive' => false,
                'order_by_metadata' => array(
                    'name' => $orderby,
                    'direction' => $orderad,   
                    'as' => $orderas
                    ),
		'metadata_owner_guids' => $user_ids,
                //'metadata_owner_guids' => ELGG_ENTITIES_ANY_VALUE,
	);	
//        echo json_encode($options);
	$entities = elgg_get_entities_from_metadata($options);
	
	return $entities;
}

function greengar_search_drawings_from_id($from_id, $to_id = null, $drawings_count = 10, $offset = 0, $orderby = null, $orderad = 'desc')
{
	return greengar_search_drawings($from_id, $to_id, null, null, null, null, null, null, null, null,
                                        $drawings_count, $offset, $orderby, $orderad);
}

function greengar_search_drawings_to_id($to_id, $from_id = null, $drawings_count = 10, $offset = 0, $orderby = null, $orderad = 'desc')
{
	return greengar_search_drawings($from_id, $to_id, null, null, null, null, null, null, null, null,
                                        $drawings_count, $offset, $orderby, $orderad);
}

function greengar_search_drawings_by_title($title, $drawings_count = 10, $offset = 0, $orderby = null, $orderad = 'desc')
{
    return greengar_search_drawings(null, null, $title, null, null, null, null, null, null, null,
                                    $drawings_count, $offset, $orderby, $orderad);
}

function greengar_search_drawings_by_owners($user_ids, $drawings_count = 10, $offset = 0, $orderby = null, $orderad = 'desc')
{
	return greengar_search_drawings(null, null, null, $user_ids, null, null, null, null, null, null,
                $drawings_count, $offset, $orderby, $orderad);
}

function greengar_search_drawings_before($before_time, $after_time = null, $drawings_count = 10, $offset = 0, $orderby = null, $orderad = 'desc')
{
	return greengar_search_drawings(null, null, null, null, $before_time, $after_time, null, null, null, null,
                $drawings_count, $offset, $orderby, $orderad);
}

function greengar_search_drawings_after($after_time, $before_time = null, $drawings_count = 10, $offset = 0, $orderby = null, $orderad = 'desc')
{
	return greengar_search_drawings(null, null, null, null, $before_time, $after_time, null, null, null, null,
                $drawings_count, $offset, $orderby, $orderad);
}

function greengar_search_drawings_by_model($device_model, $drawings_count = 10, $offset = 0, $orderby = null, $orderad = 'desc')
{
	return greengar_search_drawings(null, null, null, null, null, null, $device_model, null, null, null,
                $drawings_count, $offset, $orderby, $orderad);
}

function greengar_search_drawings_by_device_uuid($device_uuid, $drawings_count = 10, $offset = 0, $orderby = null, $orderad = 'desc')
{
	return greengar_search_drawings(null, null, null, null, null, null, null, $device_uuid, null, null,
                $drawings_count, $offset, $orderby, $orderad);
}

function greengar_search_drawings_by_location($location, $drawings_count = 10, $offset = 0, $orderby = null, $orderad = 'desc')
{
	return greengar_search_drawings(null, null, null, null, null, null, null, null, $location, null,
                $drawings_count, $offset, $orderby, $orderad);
}

function greengar_search_drawings_by_tags($tags, $drawings_count = 10, $offset = 0, $orderby = null, $orderad = 'desc')
{
	return greengar_search_drawings(null, null, null, null, null, null, null, null, null, $tags,
                $drawings_count, $offset, $orderby, $orderad);
}

function greengar_search_drawings_new($offset=0, $limit=10, $orderby=null, $orderad='desc', $tags=null, $ownerids=null, $mentions = null, $hightlights = null, $shared = 0)
{
    return greengar_search_drawings(null, null, null, $ownerids, null, null, null, null, null, $tags, $limit, $offset, $orderby, $orderad, $mentions, $hightlights, $shared);
}